package com.example.emergencyattempt01.activity.administrator_activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.utils.AbstractNetWorkUtils;
import com.example.emergencyattempt01.activity.utils.NetWorkUtils;
import com.example.emergencyattempt01.activity.utils.ServerMessage;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class Add_book_Activity extends AppCompatActivity {
    EditText bookNameField;
    EditText ISBNField;
    EditText authorField;
    EditText pressField;
    EditText sumNumberField;
    EditText typeField;
    EditText introductionField;

    Button confirm;
    Button cancel;

    String ISBN;
    String bookName;
    String author;
    String press;
    long sumNumber;
    String type;
    String introduction;

    String toServer;

    Handler handlerForAddBook=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case UPDATE_FAIL:
                    Toast.makeText(Add_book_Activity.this,"添加失败",Toast.LENGTH_SHORT).show();
                    break;
                case UPDATE_SUCCESS:
                    Toast.makeText(Add_book_Activity.this,"添加成功",Toast.LENGTH_SHORT).show();
                    finish();
                    break;
            }
        }
    };
    final int UPDATE_SUCCESS=0x041;
    final int UPDATE_FAIL=0x042;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book_);
        // 初始化界面
        initPage();
        // 设置取消
        setCancel();
        // 设置确定
        setConfirm();

    }

    private void setConfirm() {
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            // 收集数据
                            collectMessage();
                            // 打包数据
                            packMessage();
                            // 发送数据
                            ServerMessage serverMessage= NetWorkUtils.getInstance().create(toServer,"/add_new_book");
                            int code=serverMessage.getRespondCode();
                            if(code==200)
                            {
                              Message message=new Message();
                              message.what=UPDATE_SUCCESS;
                              handlerForAddBook.sendMessage(message);
                            }
                            else
                            {
                                Message message=new Message();
                                message.what=UPDATE_FAIL;
                                handlerForAddBook.sendMessage(message);
                            }
                        }catch (JSONException e){e.printStackTrace();}
                        catch (IOException e){e.printStackTrace();Message message=new Message();
                            message.what=UPDATE_FAIL;
                            handlerForAddBook.sendMessage(message);}
                    }
                }.start();

            }
        });
    }

    private void packMessage() throws JSONException{
        JSONObject object=new JSONObject();
        object.put("ISBN",ISBN);
        object.put("bookName",bookName);
        object.put("press",press);
        object.put("author",author);
        object.put("introduction",introduction);
        object.put("type",type);
        object.put("sumNumber",sumNumber);
        toServer=String.valueOf(object);
    }

    private void collectMessage() {
        ISBN=ISBNField.getText().toString();
        bookName=bookNameField.getText().toString();
        press=pressField.getText().toString();
        author=authorField.getText().toString();
        type=typeField.getText().toString();
        introduction=introductionField.getText().toString();
        sumNumber =Long.parseLong(sumNumberField.getText().toString());
    }

    private void setCancel() {
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initPage() {
        bookNameField=findViewById(R.id.ad_add_book_name);
        ISBNField=findViewById(R.id.ad_add_book_ISBN);
        authorField=findViewById(R.id.ad_add_book_author);
        pressField=findViewById(R.id.ad_add_book_press);
        sumNumberField=findViewById(R.id.ad_add_book_sumNumber);
        typeField=findViewById(R.id.ad_add_book_type);
        introductionField=findViewById(R.id.ad_add_book_introduction);

        confirm=findViewById(R.id.ad_add_edit_confirm);
        cancel=findViewById(R.id.ad_add_edit_cancel);
    }
}